﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace LDSP.Util
{
    public class DownloadHistoryExport
    {
        public static void Export(HttpServerUtility server, DataTable source)
        {
            ExcelClass excel = new ExcelClass(server.MapPath("..\\..\\Templates\\EnterpriseDownload.xls"));
            excel.ImportDataTable(excel.GetSheetByIndex(0), false, GetExportedDT(source), 3, 1);
            excel.ToExcel(@"E:\Lab\Web\ExcelImportExport\downloadHistory.xls", DownloadType.PromptDialog);
        }

        private static DataTable GetExportedDT(DataTable source)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("Enterprise");
            dt.Columns.Add("Category");
            dt.Columns.Add("DownloadDate");
            dt.Columns.Add("File");

            int index = 1;
            foreach (DataRow row in source.Rows)
            {
                string docType = row["docType"].ToString() == "0" ? "法律法规" : "标准";

                dt.Rows.Add(index,
                    row["enterpriseCode"],
                    docType,
                    row["downloadDT"],
                    row["fileName"]);

                index++;
            }

            return dt;
        }
    }
}